package com.focusai.efairy.ui.activity.project;

import android.content.Intent;
import android.graphics.BitmapFactory;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.focusai.efairy.R;
import com.focusai.efairy.model.project.ProjectItem;
import com.focusai.efairy.ui.activity.dev.DeviceListActivity;
import com.focusai.efairy.ui.activity.location.AMapLocationActivity;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.dev.ProjectLocationAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/8/31.
 */

public class ProjectMapLocationActivity extends AMapLocationActivity {
    private List<ProjectItem> mList;
    private ProjectLocationAdapter mAdapter;

    @Override
    protected void setDefaultValues() {
        super.setDefaultValues();
        setCenterTitleName("项目分布");
        if (getIntent() != null && getIntent().getExtras() != null) {
            mList = (List<ProjectItem>) getIntent().getExtras().getSerializable(KEY_MAP_ITEMS);
        }
        if (mList != null && mList.size() != 0) {
            mAdapter = new ProjectLocationAdapter(getContext());
            setAdapter(mAdapter);
            mAdapter.refreshData(mList);
            mAdapter.setOnItemViewClickListener(new ProjectLocationAdapter.OnItemViewClickListener() {
                @Override
                public void onItemClick(BaseRecyclerViewHolder holder, int position) {
                    if (markerList != null && markerList.size() > position && !markerList.get(position).isInfoWindowShown()) {
                        Marker marker = markerList.get(position);
                        aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker.getPosition())); //移动到对应位置
                        marker.showInfoWindow();
                    }
                }

                @Override
                public void onItemClickGoHere(ProjectItem item, int position) {
                    if (item != null) {
                        showMapType(item.efairyproject_location_lat, item.efairyproject_location_lng, item.efairyproject_address);
                    }
                }
            });
        } else {
            showToast("项目列表暂无数据");
        }
    }

    @Override
    protected void addMarkersToMap() {
        ArrayList<MarkerOptions> markerOptionlst = new ArrayList<>();
        if (mList != null) {
            for (ProjectItem item : mList) {
                double dlong = item.efairyproject_location_lng;
                double dlat = item.efairyproject_location_lat;
                MarkerOptions markerOption = new MarkerOptions();
                markerOption.position(new LatLng(dlat, dlong));
                markerOption.title(item.efairyproject_name);
                markerOption.snippet(item.efairyproject_address);
                markerOption.draggable(true);
                markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.map_display_icon)));
                markerOptionlst.add(markerOption);
            }
            markerList = aMap.addMarkers(markerOptionlst, true);
            mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (markerList != null && markerList.size() > 0) {
                        Marker marker = markerList.get(0);
                        aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker.getPosition())); //移动到对应位置
                        marker.showInfoWindow();
                    }
                }
            }, 1500);
        }
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        int position = markerList.indexOf(marker);
        if (position < mList.size()) {
            ProjectItem projectItem = mList.get(position);
            Intent intent = new Intent(getContext(), DeviceListActivity.class);
            intent.putExtra(DeviceListActivity.PROJECT_ITEM, projectItem);
            startActivity(intent);
        }
        super.onInfoWindowClick(marker);
    }
}
